package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.LinkedHashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;

@SuppressLint("SimpleDateFormat")
public class RateOrderWSControl extends BaseWSControl {
	public String orderId, note;
	public int score;

	public RateOrderWSControl(Context context,
			WebServiceCommunicatorListener listener, String orderId, int score,
			String note) {
		super(context, listener, WebServiceFlag.REMOVE_CARD);
		this.orderId = orderId;
		this.score = score;
		this.note = note;
	}

	@Override
	protected Object doInBackground(Void... params) {
		// Get local database

		String url = Statics.WASHIO_WS_URL + "orders/rating/" + orderId;
		LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
		parameters.put("score", score + "");
		if (note.trim().length() > 0) {
			parameters.put("note", note);
		}
		try {
			String respone = NetworkUtils.doPostRequest(url, parameters);
			JSONObject jsonObject = new JSONObject(respone);
			try {
				mError = jsonObject.getString("error");
			} catch (Exception e) {
				return "Successful";
			}

		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (JSONException e) {
			mError = e.getMessage();
			e.printStackTrace();
		}
		return mError;
	}

}
